//
//  FCRelationDetailEmployeeTableViewCell.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/22.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCRelationDetailEmployeeTableViewCell.h"
#import "FCConst.h"
#import "Define.h"
#import "Masonry.h"
#import "FCRelationBindEmployeeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "CustomerRelationHeader.h"
#import "UIViewController+runtimeCheck.h"
#import "UIViewController+returnBlock.h"
#import "FCCrmSupplyRelationOnlyTypeManagerModel.h"

@interface FCRelationDetailEmployeeTableViewCell () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *employeesArr;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation FCRelationDetailEmployeeTableViewCell
#define cumtomRowHeight 78

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier title:(NSString *)title employees:(NSArray<FCRelationRequestEmployeeModel *> *)employees isAddEmployee:(BOOL)isAddEmployee{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.employeesArr = employees;

        UILabel *titleLabel = [UILabel new];
        [self addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(12);
            make.left.mas_equalTo(16);
        }];
        titleLabel.font = Bfont17;
        titleLabel.textColor = FCTxtColor;
        titleLabel.text = title ?: @"";
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self addSubview:_tableView];
        WJCWeakSelf
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).mas_equalTo(0);
            make.left.and.right.mas_equalTo(0);
            make.height.mas_equalTo(cumtomRowHeight * weakSelf.employeesArr.count);
            if (!isAddEmployee) {
                make.bottom.mas_equalTo(0);
            }
        }];
        [self setupTableView];
        
        if (isAddEmployee) {
            UIButton *addRelationEmployeeButton = [UIButton new];
            [self addSubview:addRelationEmployeeButton];
            [addRelationEmployeeButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_tableView.mas_bottom).mas_equalTo(0);
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.height.mas_equalTo(36);
                make.bottom.mas_equalTo(-16);
            }];
            addRelationEmployeeButton.backgroundColor = FCBackColor;
            [addRelationEmployeeButton setTitle:customerRelationLocaChanged(@"+ 指派新的联系人", @"+ 指派新的联系人") forState:UIControlStateNormal];
            [addRelationEmployeeButton setTitleColor:FCBlueTxtColor forState:UIControlStateNormal];
            addRelationEmployeeButton.titleLabel.font = font14;
            [addRelationEmployeeButton addTarget:self action:@selector(addNewRelationEmployee) forControlEvents:UIControlEventTouchUpInside];
            addRelationEmployeeButton.layer.cornerRadius = 4;
        }
    }
    
    return self;
}

#pragma mark - tableView
- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = FCBackColor;
    self.tableView.rowHeight = cumtomRowHeight;
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
    self.tableView.scrollEnabled = NO;
    [self setExtraCellLineHidden:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.employeesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FCRelationRequestEmployeeModel *model = self.employeesArr[indexPath.row];
    FCEmployeeModel *employee = model.employee;
    
    FCRelationBindEmployeeTableViewCell *cell = [[FCRelationBindEmployeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:employee.PortraitUrl] placeholderImage:headPlaceImage];
    cell.nameLabel.text = employee.Name ?: @"";
    if (employee.Company.BrandName && employee.JobTitle) {
        cell.departmentLabel.text = [NSString stringWithFormat:@"%@-%@",employee.Company.BrandName ?: @"",employee.JobTitle ?: @""];
    } else{
        cell.departmentLabel.text = [NSString stringWithFormat:@"%@%@",employee.Company.BrandName ?: @"",employee.JobTitle ?: @""];
    }
    
    if (self.isShowDeleteButton) {
        if (self.employeesArr.count <= 1) {
            cell.isShowDeleteButton = NO;
        } else {
            cell.isShowDeleteButton = self.isShowDeleteButton;
        }
    }
    if (self.isShowRelationButton) {
        cell.isShowRelationButton = self.isShowRelationButton;
    }
    WJCWeakSelf
    if (self.deleteBlock) {
        cell.deleteBlock = ^{
            weakSelf.deleteBlock(indexPath.row);
        };
    }
    if (self.phoneBlock) {
        cell.phoneBlock = ^{
            weakSelf.phoneBlock(indexPath.row);
        };
    }
    if (self.talkBlock) {
        cell.talkBlock = ^{
            weakSelf.talkBlock(indexPath.row);
        };
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.didSelectRowBlock) {
        self.didSelectRowBlock(indexPath.row);
    }
}

- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    [tableView setTableFooterView:view];
}

#pragma mark - 添加新联系人
- (void)addNewRelationEmployee{
    NSMutableArray *oldEmployees = [NSMutableArray array];
    for (FCRelationRequestEmployeeModel *relEmployee in self.employeesArr) {
        [oldEmployees addObject:relEmployee.employee];
    }
    NSDictionary *cache = [FCFuChengCacheTool getHttpCacheWithUrlString:@"crmSupplyRelationOnlyTypeManager" parameters:nil];
    FCCrmSupplyRelationOnlyTypeManagerModel *relationOnlyTypeManagerModel = [FCCrmSupplyRelationOnlyTypeManagerModel mj_objectWithKeyValues: cache];
    
    UIViewController *con = [self.viewController pushAppControllerWithControllerName:@"FCChoiceEmployeeListViewController" andObjectData:@{@"nonChoicedArr":oldEmployees,@"departGroupIdArr": relationOnlyTypeManagerModel.companyDepartmentId ? @[relationOnlyTypeManagerModel.companyDepartmentId] : @[]} andTitle:customerRelationLocaChanged(@"指派我司联系人", @"指派我司联系人")];
    WJCWeakSelf
    con.idBlock = ^(NSArray<FCEmployeeModel *> *employees) {
        NSMutableArray *newEmployeeArr = [NSMutableArray array];
        for (FCEmployeeModel *employee in employees) {
            [newEmployeeArr addObject:employee.Id];
        }
        weakSelf.addNewRelationEmployeeBlock(newEmployeeArr);
    };
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
